package model;

/**
* This class describes a connection between two network nodes.
*/
public class NetworkConnection extends NetworkObject {
	public enum Side { Left, Right, Top, Bottom };
	
	public NetworkNode node1, node2;
	public Side side1, side2;
	
	/**
	* Creates a new connection.
	* @param node1 the name of the first node to be connected
	* @param side1 specifies the side of node1 to which the connection is to be attached
	* @param node2 the name of the second node to be connected
	* @param side2 specifies the side of node2 to which the connection is to be attached
	*/
//	public NetworkConnection(String node1, Side side1, String node2, Side side2) {
//		this.node1 = new NetworkNode(node1, 0, 0);
//		this.node2 = new NetworkNode(node2, 0, 0);
//		this.side1 = side1;
//		this.side2 = side2;
//	}
	
	public NetworkConnection(String node1, Side side1, String node2, Side side2, NetworkModel model) {
		this.node1 = model.getNode(node1);
		this.node2 = model.getNode(node2);
		this.side1 = side1;
		this.side2 = side2;
	}

	public NetworkConnection(NetworkNode node1, Side side1) {
		this.node1 = node1;
		this.side1 = side1;
	}

	public String toString() {
		return "C \"" + node1.getName() + "\" " + getEnumLetter(side1) + " \"" + node2.getName() + "\" " + getEnumLetter(side2);
	}
	
	public static Side getSideEnum(String side) {
		if("L".equals(side)) {
			return Side.Left;
		}
		if("R".equals(side)) {
			return Side.Right;
		}
		if("T".equals(side)) {
			return Side.Top;
		}
		if("B".equals(side)) {
			return Side.Bottom;
		}
		
		return null;
	}
	
	private String getEnumLetter(Side side) {
		if(Side.Left.equals(side)) {
			return "L";
		}
		if(Side.Right.equals(side)) {
			return "R";
		}
		if(Side.Top.equals(side)) {
			return "T";
		}
		if(Side.Bottom.equals(side)) {
			return "B";
		}
		
		return "";
	}

	public boolean containsNode(NetworkNode node) {
		if(node1.equals(node) || node2.equals(node)) {
			return true;
		}
		return false;
	}
	
	@Override
	public String getName() {
		return toString();
	}

	public int[] getControlPoints(int distance) {
		distance /= 300;
		distance = Math.min(distance, 150);
		int [] controlPoints = new int[4];
		switch (side1) {
			case Left:
				controlPoints[0] = -1 * distance;
				break;
			case Top:
				controlPoints[1] = -1 * distance;
				break;
			case Right:
				controlPoints[0] = distance;
				break;
			case Bottom:
				controlPoints[1] = distance;
				break;
		}
		switch (side2) {
			case Left:
				controlPoints[2] = -1 * distance;
				break;
			case Top:
				controlPoints[3] = -1 * distance;
				break;
			case Right:
				controlPoints[2] = distance;
				break;
			case Bottom:
				controlPoints[3] = distance;
				break;
		}
		return controlPoints;
	}
}